package com.kd.controller.jobSeeker;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.kd.bean.jobSeeker.Basic_information;
import com.kd.bean.jobSeeker.Online_resumes;
import com.kd.dao.jobSeeker.IBasic_informationDao;
import com.kd.service.jobSeeker.IOnline_resumesService;

@Controller
@RequestMapping("/online_resumes")
public class Online_resumesController {

	@Autowired
	private IOnline_resumesService nline_resumesService;

	@Autowired
	private IBasic_informationDao informationDao;

	@RequestMapping("/queryByRes_id")
	@ResponseBody
	public Map<String, Object> queryByRes_id(Online_resumes online_resumes, HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		Basic_information basic_information = (Basic_information) session.getAttribute("basicMessage");
		Online_resumes resumes = null;

		try {
			Basic_information information = informationDao.queryByBasic_id(basic_information.getBasic_id());
			if (information.getRecMessage() != null) {
				resumes = nline_resumesService.queryByRes_id(information.getRecMessage().getRes_id());
				map.put("resumes", resumes);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		map.put("resumes", resumes);
		return map;
	}

	@RequestMapping("/updateOrInsertOnline_resumes")
	public String updateOrInsertOnline_resumes(Online_resumes online_resumes, HttpSession session, Model model,
			@RequestParam("myFile") MultipartFile myFile, HttpServletRequest request) {
		Boolean flag = false;
		Basic_information basic_information = (Basic_information) session.getAttribute("basicMessage");

		// 1、设置上传路径
		String storPath = System.getProperty("user.dir") + "/data/tmp/image";
		// 2、判断该位置存在不存在
		File file = new File(storPath);
		if (!file.getParentFile().exists()) {// 如果父路径不存在
			file.mkdirs();// 则创建目录
		}
		// 3、将文件上传到上传路径
		try {
			File file2 = new File(storPath, myFile.getOriginalFilename());
			myFile.transferTo(file2);
			online_resumes.setRes_photo(myFile.getOriginalFilename());
			
			if (basic_information.getRes_id() != null || basic_information.getRecMessage()!=null) {
				flag = nline_resumesService.updateOnline_resumes(online_resumes);
			} else {
				flag = nline_resumesService.insertOnline_resumes(online_resumes, basic_information.getBasic_id());
				if(flag) {
					Basic_information information = informationDao.basicLogin(basic_information);
					session.setAttribute("basicMessage", information);
					model.addAttribute("msg", "操作成功");
					return "/view/resume.jsp";
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("msg", "操作失败");
		return "/view/resume.jsp";
	}
}
